package lt.marsrutai.mobile.android.map.overlay;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.FastestRoutesResultMapActivity.RouteNode;
import lt.marsrutai.mobile.android.service.TransportInfoResolver.TransportInfo;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * View representing a balloon with a title and text snippet.
 * 
 * @author Justas
 */
public class RoutePointBalloonView extends BalloonView<RoutePointOverlayItem> {
    
    private TextView transportId1;
    private TextView transportId2;
    private ImageView transportIdSeparator;
    
    public RoutePointBalloonView(Context context) {
        super(context);
    }
    
    public RoutePointBalloonView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RoutePointBalloonView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected int getLayout() {
        return R.layout.map_route_balloon;
    }

    protected void init(Context context) {
        super.init(context);

        transportId1 = (TextView) layout.findViewById(R.id.transportId1);
        transportId2 = (TextView) layout.findViewById(R.id.transportId2);
        transportIdSeparator = (ImageView) layout.findViewById(R.id.transportIdSeparator);
    }

    public void setData(RoutePointOverlayItem item) {
        super.setData(item);
        RouteNode routeNode = item.getRouteNode();
        
        transportId1.setVisibility(GONE);
        transportId2.setVisibility(GONE);
        transportIdSeparator.setVisibility(GONE);
        
        switch (routeNode.getType()) {
        case RIDE:
            fillViewInfo(transportId1, routeNode.getScheduleName(), routeNode.getTransportId(), item.getScopeFolder());
            break;
        case CHANGE:
            fillViewInfo(transportId1, routeNode.getScheduleName(), routeNode.getTransportId(), item.getScopeFolder());
            transportIdSeparator.setVisibility(VISIBLE);
            fillViewInfo(transportId2, routeNode.getScheduleName2(), routeNode.getTransportId2(), item.getScopeFolder());
            break;
        case GET_IN:
            transportIdSeparator.setVisibility(VISIBLE);
            fillViewInfo(transportId2, routeNode.getScheduleName2(), routeNode.getTransportId2(), item.getScopeFolder());
            break;
        case GET_OUT:
            transportIdSeparator.setVisibility(VISIBLE);
            fillViewInfo(transportId1, routeNode.getScheduleName(), routeNode.getTransportId(), item.getScopeFolder());
            break;
        }
    }

    protected void fillViewInfo(TextView view, String scheduleName, String transportId, String scopeFolder) {
        if (scheduleName == null) {
            view.setVisibility(GONE);
            return;
        }
        
        view.setVisibility(VISIBLE);
        view.setText(scheduleName);
        
        TransportInfo info = AppContext.getTransportInfoResolver().getTransportInfo(scopeFolder, transportId);
        if (info != null) {
            view.setBackgroundColor(info.getColor());
        }
    }
}
